package TOP101;

/**
 * @Question BM16
 * @Date 2022/11/6 22:18
 * @Solution
 */
public class BM16 {
    public ListNode deleteDuplicates (ListNode head) {
        if(head==null) {
            return null;
        }
        ListNode newHead = new ListNode(0);
        newHead.next=head;
        ListNode prev = newHead;
        ListNode cur = head;
        while(cur!=null) {
            int count=0;
            while(cur!=null && prev.next.val==cur.val) {
                cur=cur.next;
                count++;
            }
            if(count!=1) {
                prev.next=cur;
            }
            while(prev.next!=cur) {
                prev=prev.next;
            }
        }
        return newHead.next;
    }
}
